//
//  YXNameCertificationView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/30.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXNameCertificationView.h"
#import "ZZYPhotoHelper.h"
@interface YXNameCertificationView ()

@end

@implementation YXNameCertificationView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        NSArray *viewArray = [[NSBundle mainBundle]loadNibNamed:@"YXNameCertificationView" owner:self options:nil];
        self = viewArray[0];
        self.frame = frame;
        self.backgroundColor = color_backColor;
        
        self.referBtn.tag = 510;
        self.referBtn2.tag = 511;
        self.uploadBtn.tag = 520;
        self.uploadBtn2.tag = 521;
        [self.uploadBtn setImgViewStyle:(ButtonImgViewStyleTop) imageSize:(CGSizeMake(70, 50)) space:5];
        [self.uploadBtn2 setImgViewStyle:(ButtonImgViewStyleTop) imageSize:(CGSizeMake(70, 50)) space:5];
    }
    return self;
    
}

// 点击实例参照
- (IBAction)referBtnAction:(UIButton *)sender {
    self.selectReferBtnClickBlock(sender.tag -510);
    
}


// 点击上传图片
- (IBAction)uploadBtnAction:(UIButton *)sender {
    if (sender == self.uploadBtn) {
        [[ZZYPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
            UIImage *image = (UIImage *)data;
            self.picImageView.image = image;
            [self.uploadBtn setTitle:@"" forState:(UIControlStateNormal)];
            [self.uploadBtn setImage:[UIImage imageNamed:@""] forState:(UIControlStateNormal)];
//            self.imageData = [BX_NetWorkManager UIImageToBase64Str:self.picImageView.image];
            [self requestData:self.picImageView.image];
            
            
        }];
    }else  {
        [[ZZYPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
            UIImage *image = (UIImage *)data;
            self.picImageView2.image = image;
            [self.uploadBtn2 setTitle:@"" forState:(UIControlStateNormal)];
            [self.uploadBtn2 setImage:[UIImage imageNamed:@""] forState:(UIControlStateNormal)];
//            self.imageData2 = [BX_NetWorkManager UIImageToBase64Str:self.picImageView2.image];
            
            [self requestData:self.picImageView2.image];

        }];
    }
    
//    self.selectUploadBtnClickBlock(self.nameTF.text,self.imageData,self.imageData2);

}

- (void)requestData:(UIImage *)image {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         
                                                         @"text/html",
                                                         
                                                         @"image/jpeg",
                                                         
                                                         @"image/png",
                                                         
                                                         @"application/octet-stream",
                                                         
                                                         @"text/json",
                                                         
                                                         nil];
    
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://121.196.210.134:8082/mpos/wx/uploadImg.do?sessionId=%@",[YXUserInfoManager getUserInfo].sessionId];
    [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
//        UIImage *image = [UIImage imageNamed:@"gauge.png"];
        
        NSData *data = UIImagePNGRepresentation(image);
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmssSSSS";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        
        //上传的参数(上传图片，以文件流的格式)
        
        [formData appendPartWithFileData:data
         
                                    name:@"file"
         
                                fileName:fileName
         
                                mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"上传成功");
        NSString * str  =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *dict = [UILabel dictionaryWithJsonString:str];
        if (self.picImageView.image == image) {
            self.imageData = [NSString stringWithFormat:@"%@",dict[@"body"]];
        }else {
            self.imageData2 = [NSString stringWithFormat:@"%@",dict[@"body"]];
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"上传失败%@",error);
                
    }];
    
}


@end
